package com.lysj.order.app.controller;

import cn.hutool.core.date.DateUtil;
import com.lysj.admin.utils.constant.CommonConstant;
import com.lysj.admin.utils.exception.BaseException;
import com.lysj.admin.utils.spring.foundation.base.BaseContent;
import com.lysj.admin.utils.web.ParamUtil;
import com.lysj.admin.utils.web.Resp;
import com.lysj.order.app.vo.MctOrderVo;
import com.lysj.order.order.domain.Order;
import com.lysj.order.order.domain.OrderItem;
import com.lysj.order.order.feign.DeskFeign;
import com.lysj.order.order.service.OrderItemService;
import com.lysj.order.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ Author ：drj.
 * @ Date  ：Created in 11:40 2019/3/28
 * @ Description:
 **/
@Slf4j
@RestController
@RequestMapping("/order/management/app")
public class AppMctController extends BaseContent {

    @Resource
    private OrderService orderService;

    @Resource
    private OrderItemService orderItemService;

    @Resource
    private DeskFeign deskFeign;

    @GetMapping("/list")
    public Resp list(String storeId, Integer status) {

        List<MctOrderVo> mctOrderVoList = new LinkedList<>();
        //查询门店订单
        List<Order> orderList = orderService.getRepository().findByStoreIdAndDelFlagAndStatus(storeId, CommonConstant.NORMAL_FLAG, status);

        Map<String, String> map = new HashMap<>();
        //如果有返回订单,获取对应门店的所有餐桌信息
        if (orderList.size() > 0) {
            map = deskFeign.getAllDeskName();
        }
        //遍历订单
        for (Order order : orderList) {
            //获取餐桌名称
            String deskName = map.get(order.getDeskId());
            //初始化订单信息
            MctOrderVo mctOrderVo = new MctOrderVo();
            mctOrderVo.setOrderNumber(order.getOrderNumber());
            mctOrderVo.setDeskName(deskName);
            mctOrderVo.setCreateTime(order.getCreateTime());
            mctOrderVo.setStatus(order.getStatus());
            //若实付金额为空,则该订单状态为已支付,获取实付金额;否则金额从菜品详情中取
            BigDecimal sumMoney = orderItemService.getRepository().summaryMoney(order.getId());
            mctOrderVo.setSumMoney(null == order.getActPayMoney() ? sumMoney : order.getActPayMoney());
            mctOrderVoList.add(mctOrderVo);
        }
        return new Resp().success(mctOrderVoList);
    }

    /*
     * @author drj
     * @date 2019-04-10 19:14
     * @Description ：根据餐桌id查看订单详情
     */
    @GetMapping("/find_by_desk_id_order_detail")
    public Resp findByDeskIdOrderDetail(String deskId) {
        Order order = orderService.getRepository().findByDeskIdAndStatusAndDelFlag(deskId, Order.Status.PLACEORDER.getCode(), CommonConstant.NORMAL_FLAG);
        if (ParamUtil.isBlank(order)) {
            return new Resp().error(Resp.Status.PARAM_ERROR, "查看订单详情异常");
        }
        //返回订单详情
        return new Resp().success(orderService.orderDetail(order.getId()));
    }

    /*
     * @author drj
     * @date 2019-04-12 15:17
     * @Description :根据订单id查看订单详情
     */
    @GetMapping("/find_by_order_id_order_detail")
    public Resp orderDetail(String orderId) {
        Order order = orderService.findOne(orderId);
        if (ParamUtil.isBlank(order)) {
            return new Resp().error(Resp.Status.PARAM_ERROR, "查看订单详情异常");
        }
        //返回订单详情
        return new Resp().success(orderService.orderDetail(order.getId()));
    }


    /*
     * @author drj
     * @date 2019-04-10 19:15
     * @Description :点击下单支付
     */
    @PostMapping("/unifiedorder")
    public Resp unifiedorder(String orderId, Integer payWay) {
        return new Resp().success(orderService.orderPay(orderId, payWay));
    }

    /*
     * @author drj
     * @date 2019-04-12 16:53
     * @Description:赠菜或退菜的接口
     */
    @PostMapping("/gift_or_cancle")
    public Resp giftOrCancle(String orderItemId, Integer status) {
        return new Resp().success(orderItemService.giftOrCancle(orderItemId, status));
    }

    /*
     * @author drj
     * @date 2019-04-12 20:28
     * @Description :查看门店今日营收,订单数
     */
    @GetMapping("/find_store_daily_finance")
    public Resp findStoreDailyFinance(String storeId) throws ParseException {
        Map<String, Object> map = new HashMap<>();
        Date beginDate = DateUtil.beginOfDay(new Date());
        Date endDate = DateUtil.endOfDay(new Date());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");//Date指定格式：yyyy-MM-dd HH:mm:ss:SSS

        //查询门店今日订单数量
        Integer orderAmount = orderService.getRepository().
                countByStoreIdAndStatusAndDelFlagAndCreateTimeBetween(storeId, Order.Status.SUCCESSPAY.getCode(), CommonConstant.NORMAL_FLAG, beginDate, endDate);
        //统计门店今日营收
        BigDecimal dailyMoney = orderService.getRepository().summaryDailyMoney(storeId, simpleDateFormat.parse(simpleDateFormat.format(beginDate)), simpleDateFormat.parse(simpleDateFormat.format(endDate)));
        map.put("dailyMoney", null==dailyMoney?BigDecimal.ZERO:dailyMoney);
        map.put("orderAmount", orderAmount);
        return new Resp().success(map);
    }

}
